﻿using Castle.DynamicProxy;
using NLog;
using System.Linq;

namespace JW.Web.Base
{
    /// <summary>
    /// 拦截
    /// </summary>
    public class LogInterceptor : IInterceptor
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public void Intercept(IInvocation invocation)
        {
            logger.Info($"你正在调用方法 {invocation.Method.Name}  参数是 {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())}... ");

            try
            {
                invocation.Proceed();
            }
            catch
            {
                logger.Error($"你正在调用方法 {invocation.Method.Name}  参数是 {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())}... ");
            }

            //if (invocation.ReturnValue != null && invocation.ReturnValue is string)
            //{
            //    //在返回接口上拼上LogInterceptor
            //    invocation.ReturnValue += " LogInterceptor";
            //}
            logger.Info($"方法执行完毕，返回结果：{invocation.ReturnValue}");

            logger.Info("开始记录日志....");
        }
    }
}